﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Newtonsoft.Json;

namespace BrightcoveAPI
{   
    public class BCPlayList : BCObject
    {
        #region Private Properties
        
        private long _id;
        private long _accountId;

        #endregion Private Properties

        #region Public Properties

        // <summary>
        // A number that uniquely identifies this Playlist. This id is automatically assigned when the Playlist is created.
        // </summary>
        public long id { get { return _id; } }

        // <summary>
        // A user-specified id that uniquely identifies this Playlist.
        // </summary> 
        public String referenceId { get; set; }

        // <summary>
        // A number that uniquely identifies the account to which this Playlist belongs, assigned by Brightcove.
        // </summary>
        public long accountId { get { return _accountId; } }

        // <summary>
        // The title of this Playlist.
        // </summary> 
        public String name { get; set; }

        // <summary>
        // A short description describing this Playlist, limited to 256 characters.
        // </summary> 
        public String shortDescription { get; set; }

        // <summary>
        // A dictionary of the Video objects that are encapsulated in this Playlist.
        // </summary> 
        public Dictionary<long, BCVideo> videos = new Dictionary<long, BCVideo>();

        // <summary>
        // The URL to the thumbnail image associated with this Playlist. Thumbnails are 120x90 pixels.
        // </summary> 
        public String thumbnailURL { get; set; }

        // <summary>
        // Options are OLDEST_TO_NEWEST, NEWEST_TO_OLDEST, and ALPHABETICAL 
        // (each of which is a smart playlist, ordered as indicated) or EXPLICIT (a manual playlist).
        // </summary> 
        public BCPlayListType playlistType { get; set; }

        #endregion Public Properties

        #region Constructor

        public BCPlayList(JsonReader reader)
        {
            reader.Read();
            while (reader.TokenType != JsonToken.EndObject)
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    switch (reader.Value.ToString())
                    {
                        case "error":
                            reader.Read();
                            Console.WriteLine(String.Format("Error: {0}", reader.Value));
                            break;

                        case "id":
                            reader.Read();
                            _id = (long)reader.Value;
                            break;

                        case "referenceId":
                            reader.Read();
                            referenceId = (string)reader.Value;
                            break;

                        case "accountId":
                            reader.Read();
                            _accountId = (long)reader.Value;
                            break;

                        case "name":
                            reader.Read();
                            name = (string)reader.Value;
                            break;

                        case "shortDescription":
                            reader.Read();
                            shortDescription = (string)reader.Value;
                            break;

                        //Not yet implemented
                        case "videoIds":
                            reader.Read();
                            //
                            break;

                        //Not yet implemented
                        case "videos":
                            reader.Read();
                            //
                            break;

                        case "playlistType":
                            reader.Read();
                            String stype = (string)reader.Value;
                            switch (stype)
                            {
                                case "OLDEST_TO_NEWEST":
                                    playlistType = BCPlayListType.OLDEST_TO_NEWEST;
                                    break;

                                case "NEWEST_TO_OLDEST":
                                    playlistType = BCPlayListType.NEWEST_TO_OLDEST;
                                    break;

                                case "ALPHABETICAL":
                                    playlistType = BCPlayListType.ALPHABETICAL;
                                    break;

                                case "PLAYSTRAILING ":
                                    playlistType = BCPlayListType.PLAYSTRAILING;
                                    break;

                                case "PLAYSTRAILINGWEEK ":
                                    playlistType = BCPlayListType.PLAYSTRAILINGWEEK;
                                    break;

                                case "EXPLICIT":
                                    playlistType = BCPlayListType.EXPLICIT;
                                    break;
                            }
                            break;

                        default:
                            reader.Read();
                            break;
                    }
                }
                reader.Read();
            }
            reader.Read();
        }

        #endregion Constructor
    }
}
